﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 美妆产品管理系统
{
    public partial class Select_Order : Form
    {
        public Select_Order()
        {
            InitializeComponent();
        }

        public int pageSize = 15;
        public int recordCount = 0;
        public int pageCount = 0;
        public int currentPage = 1;

        SqlDataAdapter daLog;
        DataSet ds = new DataSet();

        void init()
        {
            DB.GetCn();
            string str = "select * from Tlog";
            daLog = new SqlDataAdapter(str, DB.cn);
            daLog.Fill(ds, "log_info");
            DB.cn.Close();
        }
        void showAll()
        {
            DataView dvLog = new DataView(ds.Tables["log_info"]);
            dgvLog.DataSource = dvLog;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            currentPage--;
            LoadPage();
        }

        private void Select_Order_Load(object sender, EventArgs e)
        {
            init();
            LoadPage();
        }

        private void LoadPage()
        {
            recordCount = ds.Tables["log_info"].Rows.Count;
            pageCount = recordCount / pageSize;
            if ((recordCount % pageSize) > 0)
            {
                pageCount++;
            }
            if (currentPage < 1)
            {
                currentPage = 1;
            }
            if (currentPage > pageCount)
            {
                currentPage = pageCount;
            }
            int beginRecord;
            int endRecord;
            DataTable dt = ds.Tables["log_info"];
            DataTable st = new DataTable();
            st = dt.Clone();
            beginRecord = pageSize * (currentPage - 1);
            endRecord = pageSize * currentPage;
            if (currentPage == pageCount)
            {
                endRecord = recordCount;
            }
            for(int i = beginRecord; i < endRecord; i++)
            {
                st.ImportRow(dt.Rows[i]);
            }
            dgvLog.DataSource = st;
            label2.Text = currentPage.ToString();
            label4.Text = pageCount.ToString();
            label6.Text = recordCount.ToString();
        }

        private void btnpgup_Click(object sender, EventArgs e)
        {
            currentPage = 1;
            LoadPage();
        }

        private void btnpgDown_Click(object sender, EventArgs e)
        {
            currentPage = pageCount;
            LoadPage();
        }

        private void btnnext_Click(object sender, EventArgs e)
        {
            currentPage++;
            LoadPage();
        }

        private void btnCance_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
